Repository navigation
Add [NonCopyable] for SF002 and make ReadOnlySequenceReadBuffer's sequence parameter scoped - #7
Merged
Merged
Conversation
A serializer's reader or writer is not a buffer itself but carries one by value, so copying it duplicates the buffer just as copying the buffer would. Implementing IWriteBuffer/IReadBuffer stays the implicit marker; structs marked with SerializerFoundation.CodeAnalysis.NonCopyableAttribute now get the same SF002 checks. The analyzer keys on this library's attribute type, not the name, and reads it from the generic definition so every construction is covered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The constructors copy the sequence and never keep a reference to it, but an unscoped `in` parameter on a ref struct constructor tells the compiler the result may capture that reference. Storing a buffer built from a by-value parameter or a local into a field then failed with CS8347, forcing callers to launder the reference through MemoryMarshal. The new test holds the buffer in a field the same way a serializer reader does and fails to compile without `scoped`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two changes requested by the MessagePack for C# v4 work, which is building a reader and a writer that own a SerializerFoundation buffer.
[NonCopyable]for SF002A serializer's reader or writer is not a buffer, but it holds one by value. Copying it duplicates the inner buffer: two write indexes over one window, and pooled arrays returned twice at Dispose. SF002 could not see this because it only keys on
IWriteBuffer/IReadBuffer.SerializerFoundation.CodeAnalysis.NonCopyableAttribute(AttributeTargets.Struct), next to[RequireOverride].inparameters, assignment, boxing, tuples,foreach, patterns, and so on.Writer<TBuffer>covers every construction.scopedon theReadOnlySequenceReadBufferconstructorsBoth constructors copy the sequence and never keep a reference to it. Without
scoped, though, aninparameter on a ref struct constructor tells the compiler the result may capture that reference. Storing a buffer built from a by-value parameter or a local into a field then failed with CS8347, and callers had to work around it by passing the reference throughMemoryMarshal.CreateReadOnlySpan.scoped in ReadOnlySequence<byte>.CompatibleReadOnlySequenceReadBufferis unchanged. It is not a ref struct, so itsinparameter can't be captured.ScopedRefAttributeis emitted for netstandard2.0, netstandard2.1 and net9.0.Notes for review
ReadOnlySequenceReadBufferScopedTestis a compile-time regression check. Withscopedremoved, the file fails to build with CS8347 (verified locally). It also uses[NonCopyable]through the real analyzer.🤖 Generated with Claude Code